from datetime import datetime
from app.models.mysql_db import db

class UserDevice(db.Model):
    """用户与设备的关联表"""
    __tablename__ = 'user_devices'
    
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('administrator.id'), nullable=False)
    device_id = db.Column(db.Integer, db.ForeignKey('devices.id'), nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    
    # 添加关系
    user = db.relationship('User', backref=db.backref('devices', lazy='dynamic'))
    device = db.relationship('Device', backref=db.backref('users', lazy='dynamic'))
    
    def __init__(self, user_id, device_id):
        self.user_id = user_id
        self.device_id = device_id